<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>

          <a-col :span="12">
            <a-form-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag :disabled="disableSubmit" type="list" v-decorator="['typeId', validatorRules.typeId]"
                                 :trigger-change="true"
                                 dictCode="DataDictionary,description,dictionaryCode,dictionaryTypeId = COMPANY_TYPE"
                                 placeholder="请选择公司类型"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="公司名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="disableSubmit" v-decorator="['companyName',validatorRules.companyName]"
                       placeholder="请输入公司名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="法人姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="disableSubmit" v-decorator="['legalName',validatorRules.legalName]"
                       placeholder="请输入法人姓名"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="法人身份证号码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="disableSubmit" v-decorator="['legalIdCard', validatorRules.legalIdCard]"
                       placeholder="请输入法人身份证号码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="税号 " :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="disableSubmit" v-decorator="['taxNumber',validatorRules.taxNumber]"
                       placeholder="请输入税号 "></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="开票地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="disableSubmit" v-decorator="['invoiceAddress']" placeholder="请输入开票地址"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="开票电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="disableSubmit" v-decorator="['invoicePhone']" placeholder="请输入开票电话"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="开户银行" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="disableSubmit" v-decorator="['bankName']" placeholder="请输入开户银行"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="银行账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="disableSubmit" v-decorator="['bankAccount']" placeholder="请输入银行账号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="邮寄地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="disableSubmit" v-decorator="['mailingAddress']" placeholder="请输入邮寄地址"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="公司电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="disableSubmit" v-decorator="['companyPhone']" placeholder="请输入公司电话"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="公司官网" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="disableSubmit" v-decorator="['companyWebsite']" placeholder="请输入公司官网"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="公司级别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag :disabled="disableSubmit" type="list" v-decorator="['levelId', validatorRules.levelId]"
                                 :trigger-change="true"
                                 dictCode="DataDictionary,description,dictionaryCode,dictionaryTypeId = CUST_LEVEL"
                                 placeholder="请选择公司级别"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="所属行业" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag :disabled="disableSubmit" type="list"
                                 v-decorator="['industryId', validatorRules.industryId]" :trigger-change="true"
                                 dictCode="DataDictionary,description,dictionaryCode,dictionaryTypeId = CUST_INDUSTRY"
                                 placeholder="请选择所属行业"/>
            </a-form-item>
          </a-col>
          <!-- <a-col :span="12">
                  <a-form-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
                    <a-input v-decorator="['address']" placeholder="请输入地址"></a-input>
                  </a-form-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea :disabled="disableSubmit" v-decorator="['detailedAddress',validatorRules.detailedAddress]"
                          rows="4" placeholder="请输入详细地址"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea :disabled="disableSubmit" v-decorator="['description']" rows="4" placeholder="请输入备注"/>
            </a-form-item>
          </a-col>

        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import {httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import {validateDuplicateValue} from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: "ClmCompanyModal",
    components: {
      JDictSelectTag,
    },
    data () {
      return {
        disableSubmit: false,
        form: this.$form.createForm(this),
        title: "操作",
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 7},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        confirmLoading: false,
        validatorRules: {
          /*legalIdCard:{
           rules: [{
           required: true,
           pattern:/^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
           message: '请输入正确的身份证号码!'
           }]
           },
           legalName:{
           rules: [{
           required: true,
           message: '请输入法人姓名!'
           }],
           },
           taxNumber:{
           rules: [{
           required: true,
           message: '请输入法人姓名!'
           }],
           }, */
          companyName: {
            rules: [{
              required: true,
              message: '请输入公司名称!'
            }],
          },
          taxNumber: {
            rules: [{
              required: true,
              message: '请输入税号!'
            }],
          },
          detailedAddress: {
            rules: [{
              required: true,
              message: '请输入详细地址!'
            }],
          },
        },
        url: {
          add: "/online/api/createClmCompany",
          edit: "/online/api/updateClmCompany",
          get: "/online/api/getClmCompany",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.visible = true;
        this.model = {};
        if (record.id) {
          httpAction(this.url.get, {id: record.id}, "post").then((res) => {
            if (res.success) {
              this.model = Object.assign({}, res.result);
              this.$nextTick(() => {
                this.form.setFieldsValue(pick(this.model, 'typeId', 'companyName', 'legalName', 'legalIdCard', 'taxNumber', 'invoiceAddress', 'invoicePhone', 'bankName','bankAccount','mailingAddress', 'companyPhone', 'companyWebsite', 'levelId', 'industryId', 'address', 'detailedAddress', 'description', 'createDate', 'createBy', 'statusId'))
              })
            }
          })
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据", formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success('提交成功!');
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row, 'typeId', 'companyName', 'legalName', 'legalIdCard', 'taxNumber', 'invoiceAddress', 'invoicePhone', 'bankName','bankAccount','mailingAddress', 'companyPhone', 'companyWebsite', 'levelId', 'industryId', 'address', 'detailedAddress', 'description', 'createDate', 'createBy', 'statusId'))
      },


    }
  }
</script>
